package com.zjw.exception;

import com.zjw.constant.ResultCodeEnum;
import com.zjw.util.ResultGenUtil;
import com.zjw.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import java.sql.SQLException;

/**
 * @author zjw
 * @date 2018/3/7
 */
@RestControllerAdvice
@Component
@Slf4j
public class GlobalExceptionHandler {

    /**
     * IllegalArgumentException异常处理返回json
     * <p>
     * 返回状态码:400
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler({IllegalArgumentException.class})
    public ResultVO<Object> badRequestException(IllegalArgumentException e) {
        return ResultGenUtil.genFail(ResultCodeEnum.ILLEGAL_ARGUMENT_EXCEPTION, e);
    }

    /**
     * 不支持当前请求方法
     * <p>
     * 返回状态码:405
     */
    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
    @ExceptionHandler({HttpRequestMethodNotSupportedException.class})
    public ResultVO<Object> handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
        return ResultGenUtil.genFail(ResultCodeEnum.METHOD_NOT_ALLOWED_EXCEPTION, e);
    }

    /**
     * 不支持的媒体类型
     * <p>
     * 返回状态码:415
     */
    @ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
    @ExceptionHandler({HttpMediaTypeNotSupportedException.class})
    public ResultVO<Object> handleHttpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException e) {
        return ResultGenUtil.genFail(ResultCodeEnum.HTTP_MEDIA_TYPE_NOT_SUPPORTED_EXCEPTION, e);
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResultVO<Object> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        BindingResult bindingResult = e.getBindingResult();
        return ResultGenUtil.genCustom(ResultCodeEnum.ARGUMENTS_NOT_VALID_ERROR.getCode(),
                bindingResult.getAllErrors().get(0).getDefaultMessage());
    }

    /**
     * 找不到请求路径对应的处理
     * <p>
     * 返回状态码:500
     */
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler({NoHandlerFoundException.class})
    public ResultVO handleNoHandlerFoundException(SQLException e) {
        log.error("找不到请求路径对应的处理", e);
        return ResultGenUtil.genFail(ResultCodeEnum.NO_HANDLER_FOUND, e);
    }

    /**
     * 业务处理异常
     * <p>
     * 返回状态码:500
     */
    @ExceptionHandler({BusinessException.class})
    public ResultVO<Object> handleNoHandlerFoundException(BusinessException e) {
        return ResultGenUtil.genFail(e.getResultCodeEnum());
    }

    /**
     * 其他异常
     * <p>
     * 返回状态码:500
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler({Exception.class})
    public ResultVO<Object> handleHttpMessageNotReadableException(Exception e) {
        log.error("遇到未捕获错误，具体错误原因:", e);
        return ResultGenUtil.genFail(ResultCodeEnum.UNKNOWN_ERROR);
    }
}